package fr.afcepf.atod16.groupe1.projet1.entities.catalogue;

import fr.afcepf.atod16.groupe1.projet1.entities.Entite;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;

/**
 * L'entité promo
 * @author cedric
 */
@Entity
@Table(name = "promos")
public class Promo implements Serializable, Entite {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;
    @Column(name="date_debut", nullable = false)
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateDebut;
    @Column(name="date_fin", nullable = false)
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateFin;
    @Column(name="remise", nullable = false)
    private Integer remise;
    @ManyToMany
    @JoinTable(name = "promos_produits",
    joinColumns = {
        @JoinColumn(name = "id_promo")},
    inverseJoinColumns = {
        @JoinColumn(name = "id_produit")})
    private List<Produit> produits;

    public Promo() {
    }

    public Promo(Integer id, Date dateDebut, Date dateFin, Integer remise,
            List<Produit> produits) {
        this.id = id;
        this.dateDebut = dateDebut;
        this.dateFin = dateFin;
        this.remise = remise;
        this.produits = produits;
    }

    @Override
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Date getDateDebut() {
        return dateDebut;
    }

    public void setDateDebut(Date dateDebut) {
        this.dateDebut = dateDebut;
    }

    public Date getDateFin() {
        return dateFin;
    }

    public void setDateFin(Date dateFin) {
        this.dateFin = dateFin;
    }

    public Integer getRemise() {
        return remise;
    }

    public void setRemise(Integer remise) {
        this.remise = remise;
    }

    public List<Produit> getProduits() {
        return produits;
    }

    public void setProduits(List<Produit> produits) {
        this.produits = produits;
    }

    @Override
    public String toString() {
        return "Promo [id=" + id + ", dateDebut=" + dateDebut + ", dateFin="
                + dateFin + ", remise=" + remise + ", produits=" + produits
                + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((dateDebut == null) ? 0 : dateDebut.hashCode());
        result = prime * result + ((dateFin == null) ? 0 : dateFin.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result
                + ((produits == null) ? 0 : produits.hashCode());
        result = prime * result + ((remise == null) ? 0 : remise.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Promo other = (Promo) obj;
        if (dateDebut == null) {
            if (other.dateDebut != null) {
                return false;
            }
        } else if (!dateDebut.equals(other.dateDebut)) {
            return false;
        }
        if (dateFin == null) {
            if (other.dateFin != null) {
                return false;
            }
        } else if (!dateFin.equals(other.dateFin)) {
            return false;
        }
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        if (produits == null) {
            if (other.produits != null) {
                return false;
            }
        } else if (!produits.equals(other.produits)) {
            return false;
        }
        if (remise == null) {
            if (other.remise != null) {
                return false;
            }
        } else if (!remise.equals(other.remise)) {
            return false;
        }
        return true;
    }
}
